package com.study.web.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.study.common.constant.Constants;
import com.study.common.core.domain.R;
import com.study.common.core.domain.model.AppEmailLoginBody;
import com.study.common.core.domain.model.AppLoginBody;
import com.study.common.core.domain.model.AppSmsLoginBody;
import com.study.common.core.domain.model.AppThirdLoginBody;
import com.study.common.enums.LoginType;
import com.study.app.domain.AppUser;
import com.study.app.service.IAppLoginService;
import com.study.common.utils.MessageUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录验证
 *
 * @author mjzx
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping
public class LoginController {

    private final IAppLoginService iAppLoginService;

    /**
     * 登录方法
     *
     * @param appLoginBody 登录信息
     * @return 结果
     */
    @SaIgnore
    @PostMapping("/login")
    public R<Map<String, Object>> login(@Validated @RequestBody AppLoginBody appLoginBody) {
//        appLoginBody.setPassword(iAppLoginService.decrypt(appLoginBody.getPassword()));

        Map<String, Object> ajax = new HashMap<>(1);
        // 生成令牌
        String token = iAppLoginService.login(appLoginBody.getUsername(), appLoginBody.getPassword());
        //获取当前登陆用户信息
        AppUser appUser = iAppLoginService.getAppUser(appLoginBody.getUsername());
        ajax.put(Constants.TOKEN, token);
        ajax.put("user", appUser);
        return R.ok(ajax);
    }

    /**
     * 短信登录
     *
     * @param appSmsLoginBody 登录信息
     * @return 结果
     */
    @SaIgnore
    @PostMapping("/smsLogin")
    public R<Map<String, Object>> smsLogin(@Validated @RequestBody AppSmsLoginBody appSmsLoginBody) {
        Map<String, Object> ajax = new HashMap<>(1);
        // 生成令牌
        String token = iAppLoginService.smsLogin(appSmsLoginBody);
        //获取当前登陆用户信息
        AppUser appUser = iAppLoginService.getAppUser(appSmsLoginBody.getPhoneNumber());
        ajax.put(Constants.TOKEN, token);
        ajax.put("user", appUser);
        return R.ok(ajax);
    }

    /**
     * 邮箱登录
     *
     * @param appEmailLoginBody 登录信息
     * @return 结果
     */
    @SaIgnore
    @PostMapping("/emailLogin")
    public R<Map<String, Object>> emailLogin(@Validated @RequestBody AppEmailLoginBody appEmailLoginBody) {
        appEmailLoginBody.setPassword(iAppLoginService.decrypt(appEmailLoginBody.getPassword()));
        Map<String, Object> ajax = new HashMap<>(1);
        // 生成令牌
        String token = iAppLoginService.emailLogin(appEmailLoginBody);
        //获取当前登陆用户信息
        AppUser appUser = iAppLoginService.getAppUser(appEmailLoginBody.getEmail());
        ajax.put(Constants.TOKEN, token);
        ajax.put("user", appUser);
        return R.ok(ajax);
    }

    /**
     * 微信登录
     *
     * @param appThirdLoginBody 登录信息
     * @return 结果
     */
    @SaIgnore
    @PostMapping("/wechatLogin")
    public R<Map<String, Object>> wechatLogin(@Validated @RequestBody AppThirdLoginBody appThirdLoginBody) {
        Map<String, Object> ajax = new HashMap<>(1);
        // 生成令牌
        String token = iAppLoginService.thirdLogin(LoginType.WECHAT, appThirdLoginBody);
        ajax.put(Constants.TOKEN, token);
        return R.ok(ajax);
    }

    /**
     * QQ登录
     *
     * @param appThirdLoginBody 登录信息
     * @return 结果
     */
    @SaIgnore
    @PostMapping("/qqLogin")
    public R<Map<String, Object>> qqLogin(@Validated @RequestBody AppThirdLoginBody appThirdLoginBody) {
        Map<String, Object> ajax = new HashMap<>();
        // 生成令牌
        String token = iAppLoginService.thirdLogin(LoginType.QQ, appThirdLoginBody);
        ajax.put(Constants.TOKEN, token);
        return R.ok(ajax);
    }

    /**
     * Apple ID登录
     *
     * @param appThirdLoginBody 登录信息
     * @return 结果
     */
    @SaIgnore
    @PostMapping("/appleIdLogin")
    public R<Map<String, Object>> appleIdLogin(@Validated @RequestBody AppThirdLoginBody appThirdLoginBody) {
        Map<String, Object> ajax = new HashMap<>();
        // 生成令牌
        String token = iAppLoginService.thirdLogin(LoginType.APPLE_ID, appThirdLoginBody);
        ajax.put(Constants.TOKEN, token);
        return R.ok(ajax);
    }

    /**
     * 退出登录
     */
    @SaIgnore
    @PostMapping("/logout")
    public R<Void> logout() {
        iAppLoginService.logout();
        return R.ok(MessageUtils.message("login.logout"));
    }

}
